﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MyDataProvider;

namespace ExcelDbAddIn
{
    public partial class FormView : Form
    {
        public ColumnsInformation.ColumnsInfo columnsInfo;
        public string[] columnsForShow;
        private ContextMenu contextMenuForColumn = new ContextMenu();

        public FormView()
        {
            InitializeComponent();
        }

        private void FormView_Load(object sender, EventArgs e)
        {
            if (columnsForShow != null && columnsForShow.Count() != 0)
            {
                foreach (string column in columnsForShow)
                    listBoxSelectedColumns.Items.Add(column);
            }
            if (columnsForShow == null)
                foreach (var column in columnsInfo)
                    listBoxColumns.Items.Add(column.Name);
            else
                foreach (var column in columnsInfo)
                    if (!columnsForShow.Contains(column.Name))
                        listBoxColumns.Items.Add(column.Name);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBoxSelectedColumns.Items.Clear();
            listBoxColumns.Items.Clear();
            foreach (var column in columnsInfo)
            {
                listBoxColumns.Items.Add(column.Name);
            }
        }
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void btnOK_Click(object sender, EventArgs e)
        {           
            int cnt = listBoxSelectedColumns.Items.Count;
            if (cnt == 0)
                return;
            columnsForShow = new string[cnt];
            try
            {
                for (int i = 0; i < cnt; i++)
                    columnsForShow[i] = listBoxSelectedColumns.Items[i].ToString();
            }
            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show(ex.Message);
                throw ex;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void listBoxColumns_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxColumns.Items.Count == 0 || listBoxColumns.SelectedIndex == -1)
                btnAdd.Enabled = false;
            else
                btnAdd.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string[] array = new string[listBoxColumns.SelectedItems.Count];
            listBoxColumns.SelectedItems.CopyTo(array, 0);
            foreach (var item in array)
            {
                listBoxSelectedColumns.Items.Add(item);
                listBoxColumns.Items.Remove(item);
            }
            if (listBoxColumns.Items.Count > 0)
                listBoxColumns.SelectedIndex = 0;
            listBoxColumns.SelectedIndices.Clear();
        }

        private void listBoxSelectedColumns_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSelectedColumns.Items.Count == 0 || listBoxSelectedColumns.SelectedIndex == -1)
            {
                btnRemove.Enabled = false;
                btnUp.Enabled = false;
            }
            else
                btnRemove.Enabled = true;
            if (listBoxSelectedColumns.SelectedIndex == listBoxSelectedColumns.Items.Count - 1)
            {
                btnDown.Enabled = false;
            }
            else
                btnDown.Enabled = true;
            if (listBoxSelectedColumns.SelectedIndex == 0)
            {
                btnUp.Enabled = false;
            }
            else
                btnUp.Enabled = true;
                
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            string[] array = new string[listBoxSelectedColumns.SelectedItems.Count];
            listBoxSelectedColumns.SelectedItems.CopyTo(array, 0);
            foreach (var item in array)
            {
                listBoxColumns.Items.Add(item);
                listBoxSelectedColumns.Items.Remove(item);
            }
            if (listBoxSelectedColumns.Items.Count > 0)
                listBoxSelectedColumns.SelectedIndex = 0;
            listBoxSelectedColumns.SelectedIndices.Clear();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            listBoxSelectedColumns.SelectionMode = SelectionMode.One;
            int index = listBoxSelectedColumns.SelectedIndex;
            if (index <= 0 || listBoxSelectedColumns.Items.Count == 1)
                return;
            int prev = index - 1;
            string temp = listBoxSelectedColumns.Items[prev].ToString();
            listBoxSelectedColumns.Items[prev] = listBoxSelectedColumns.Items[index];
            listBoxSelectedColumns.Items[index] = temp;
            listBoxSelectedColumns.SelectedIndex = prev;
            listBoxSelectedColumns.SelectionMode = SelectionMode.MultiExtended;
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            listBoxSelectedColumns.SelectionMode = SelectionMode.One;
            int index = listBoxSelectedColumns.SelectedIndex;
            if (index < 0 || listBoxSelectedColumns.Items.Count == 1 || index == listBoxSelectedColumns.Items.Count - 1)
                return;
            int next = index + 1;
            string temp = listBoxSelectedColumns.Items[next].ToString();
            listBoxSelectedColumns.Items[next] = listBoxSelectedColumns.Items[index];
            listBoxSelectedColumns.Items[index] = temp;
            listBoxSelectedColumns.SelectedIndex = next;
            listBoxSelectedColumns.SelectionMode = SelectionMode.MultiExtended;
        }

        private void btnAddAll_Click(object sender, EventArgs e)
        {
            foreach (var item in listBoxColumns.Items)
            {
                listBoxSelectedColumns.Items.Add(item);
            }
            listBoxColumns.Items.Clear();
        }
        
    }
}
